package me.luger.core.mvc.interceptor.access;

import me.luger.core.commom.util.ApplicationUtil;
import me.luger.core.commom.util.HttpUtil;
import me.luger.core.commom.util.JsonUtil;
import me.luger.core.commom.util.LogUtil;
import me.luger.core.mvc.WebService;
import me.luger.core.mvc.log.access.AccessLogEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * Created by Administrator on 2016/8/15 0015.
 * 因为有用到eh page cache 所以记录访问日志改到accessFilter中
 */
//@Component
public class AccessInterceptor implements HandlerInterceptor {

    @Autowired
    private Environment env;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        AccessLogEntity logEntity = this.preHandleLog(request,response);
        WebService.setAccessLogEntity(logEntity);
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setCharacterEncoding("UTF-8");
        return true;// 只有返回true才会继续向下执行，返回false取消当前请求
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

        AccessLogEntity logEntity = WebService.getAccessLogEntity();
//        HandlerMethod handlerMethod = (HandlerMethod) handler;
//        String methodName = handlerMethod.getMethod().getName();

        logEntity.setAfterControllerTime(System.nanoTime());
        WebService.setAccessLogEntity(logEntity);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        AccessLogEntity logEntity = WebService.getAccessLogEntity();
        logEntity.setEndTime(System.nanoTime());
        logEntity.setExpend(logEntity.getEndTime() - logEntity.getBeginTime());
        LogUtil.accessLog(JsonUtil.toString(logEntity));
        WebService.clean();
    }

    private AccessLogEntity preHandleLog(HttpServletRequest request, HttpServletResponse response){
        String reqId = UUID.randomUUID().toString();
        LogUtil.setLogId(reqId);

        AccessLogEntity logEntity = new AccessLogEntity();
        logEntity.setLogId(reqId);
        logEntity.setAccessDomain(request.getServerName());
        logEntity.setAccessIp(request.getRemoteAddr());
        logEntity.setBeginTime(System.nanoTime());
        logEntity.setGzip(HttpUtil.acceptsGzipEncoding(request));
        logEntity.setHttpMethod(request.getMethod());
        logEntity.setParams(request.getParameterMap());
        logEntity.setUrl(request.getRequestURI());
        logEntity.setUserIp(HttpUtil.getRemoteAddr(request));
        logEntity.setReferer(HttpUtil.getReferer(request));

        if(null != env){
            String basePath = env.getProperty("project.basePath");
            if(null != basePath){
                request.setAttribute("base",basePath);
            }
        }

        return logEntity;
    }
}
